package com.ling.service;

import com.ling.bean.BlogExample;
import com.ling.bean.UserBlogType;
import com.ling.bean.UserBlogTypeExample;
import com.ling.dao.BlogMapper;
import com.ling.dao.UserBlogTypeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BlogTypeService {
    private final UserBlogTypeMapper userBlogTypeMapper;
    private final BlogMapper blogMapper;

    @Autowired
    public BlogTypeService(UserBlogTypeMapper userBlogTypeMapper, BlogMapper blogMapper) {
        this.userBlogTypeMapper = userBlogTypeMapper;
        this.blogMapper = blogMapper;
    }

    //增加博客类别
    public void addBlogType(UserBlogType userBlogType) {
        userBlogTypeMapper.insertSelective(userBlogType);
    }

    //删除博客类别
    public void delBlogTypeById(String userBlogTypeId) {
        userBlogTypeMapper.deleteByPrimaryKey(userBlogTypeId);
    }

    /**
     * 修改博客类别
     * 1.修改名称
     * 2.修改文章数
     */
    public void updateBlogType(UserBlogType userBlogType) {
        userBlogTypeMapper.updateByPrimaryKeySelective(userBlogType);
    }

    //查询用户的所有博客类别
    public List<UserBlogType> getUserBlogType(String userId) {
        UserBlogTypeExample userBlogTypeExample = new UserBlogTypeExample();
        //按照博客类别的名称排序,升序
        userBlogTypeExample.setOrderByClause("t_userblogtype.blogTypeName ASC");
        userBlogTypeExample.createCriteria().andUseridEqualTo(userId);
        return userBlogTypeMapper.selectByExample(userBlogTypeExample);
    }

    //判断指定id的博客类别下的文章数
    public long getCountOfBlogType(String blogTypeId) {
        BlogExample blogExample = new BlogExample();
        blogExample.createCriteria().andBlogtypeidEqualTo(blogTypeId);
        return blogMapper.countByExample(blogExample);
    }


    //查询指定id的博客类型
    public UserBlogType getUserBlogTypeById(String id) {
        return userBlogTypeMapper.selectByPrimaryKey(id);
    }
}
